package com.sinoglobal.sinostore.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.bean.AddressVo;
import com.sinoglobal.sinostore.utils.TextUtil;


public class AddressAdapter extends BaseAdapter {
	private List<AddressVo> data;
	private Context context;
	
	public AddressAdapter(Activity context,List<AddressVo> data){
		this.context = context;
		this.data = data;
	}
	
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-10 上午9:40:07
	 * @Description: (添加数据)
	 * @param data
	 *
	 */
	public void addList(List<AddressVo> data){
		if(null == data){
			return;
		}
		for(AddressVo vo : data){
			this.data.add(vo);
		}
		notifyDataSetChanged();
	}
	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-21 上午11:47:46
	 * @Description: (清空数据)
	 *
	 */
	public void clearData(){
		if(null != data){
			data.clear();
			notifyDataSetChanged();
		}
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public AddressVo getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup group) {
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.shop_activity_addresslist_item, null);
		}
		AddressVo addressVo = getItem(position);
		TextView name = ViewHolder.getViewById(convertView, R.id.name);
		TextView phone = ViewHolder.getViewById(convertView, R.id.phone);
		TextView tvaddress = ViewHolder.getViewById(convertView, R.id.address);
		TextView isDefault = ViewHolder.getViewById(convertView, R.id.isdefault);
		if("1".equals(addressVo.getIs_default())){//1 为默认收货地址
			isDefault.setVisibility(View.VISIBLE);
		}else{
			isDefault.setVisibility(View.INVISIBLE);
		}
		if(addressVo.isSelect()){
			convertView.setBackgroundColor(context.getResources().getColor(R.color.color_gray));
		}
		name.setText(addressVo.getConnect_name());
		phone.setText(addressVo.getConnect_tel());
		StringBuffer sb = new StringBuffer();
		sb.append("收货地址：").append(addressVo.getArea()).append(addressVo.getAddress());
		String _address = TextUtil.ToDBC(sb.toString());
		tvaddress.setText(_address);
		return convertView;
	}
	/**
	 * 
	* @author LK
	* @methods setItemSelect 
	* @description 选择收货地址
	* @date 2015-1-28 下午8:22:47
	* @param position 参数说明
	 */
	public void setItemSelect(int position){
		for(AddressVo address : data){
			address.setSelect(false);
		}
		data.get(position).setSelect(true);
		notifyDataSetChanged();
	}

}
